import { Http } from "../../utils/http"
import { Card } from "../../model/Card"
import { Order } from "../../model/Order"
import { config } from "../../config/config";

const app = getApp();
Page({

  //页面的初始数据
  data: {
    qrcode_url: "",
    id: "",
    orderData: [],
    statu: "",
    qrUrl: '',
    order_no:'',
    type_product:''
  },

  //生命周期函数--监听页面加载
  onLoad: async function (options) {
    let a = await Http.login();
    this.setData({
      id: options.id,
      statu: options.status,
      qrUrl: config.apiBaseUrl,
      order_no:options.order_no
    })
    
    let order = await Order.get_detail(this.data.id); //获取订单详情
    this.setData({
      type_product:order.data.type_product
    })

    this.orderStatus(order);
  },

  //获取订单详情
  orderStatus: async function (order) {
    if (order.code == 0) {
      this.setData({
        orderData: order.data
      })
      if(this.data.type_product == 1){
        let card = await Card.get_card_qrcode(this.data.id);  //权益卡 二维码
        this.onQrcodeCard(card);
      }else{
        let order_nos = await Order.get_create_mini_code(this.data.order_no); //商品 二维码
        this.onQrcode(order_nos);
      }
    } else {
      wx.showToast({
        title: order.msg,
        duration: 2000,
        icon: 'none'
      });
    }
  },

  //获取会员 - 卡 - 小程序码
  onQrcodeCard: async function (card) {
    if (card.code == 0) {
      this.setData({
        qrcode_url: card.data
      })
      if(this.data.qrcode_url==card.data){
        console.log('权益卡二维码获取成功');
      }
    } else {
      console.log("获取权益卡二维码失败");
    }
  },
  
  //获取创建订单的二维码
  onQrcode: async function (order_nos) {
    if (order_nos.code == 0) {
      this.setData({
        qrcode_url:  order_nos.data.qrcode_url
      })
      if(this.data.qrcode_url==order_nos.data.qrcode_url){
        console.log('商品二维码获取成功');
      }
    } else {
      console.log("获取商品二维码失败");
    }
  },
  
  card: function () {
    wx.navigateTo({
      url: '/pages/carddetails/index?cardId=' + this.data.orderData.id,
    })
  }
})